﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Proggy.Web.Cms.Controllers.BackEnd;
using System.Web.Mvc;
using Proggy.Web.Cms.Core.ViewModels;
using Proggy.Web.Cms.Core.Configuration.ConfigObjects;
using Proggy.Web.Cms.Core.Models;
using Proggy.Web.Cms.Core.PropertyTypes;
using Proggy.Web.Cms.Cache;

namespace Proggy.Web.Cms.Core.Controllers.PropertyType
{
    public class RteController : BackEndControllerBase
    {
        [HttpGet]
        public ActionResult LinkCreatorEditor(string text, string href, string title)
        {
            // get model
            var model = new UrlSelector
            {

                AddedUrl = this.createAddedUrlFromLink(text, href, title),
                BaseConfigObject = this._urlSelectorConfigObject

            };

            // set name start
            //model.PropertyNameStart= model.GetUniqueElementId("us_", "_us");

            //
            return View(model);
        }

        [HttpPost]
        public ActionResult LinkCreatorEditor(UrlSelector model)
        {
            // check model state
            if (ModelState.IsValid)
            {

                // check content
                if (model.AddedUrl.UrlType == AddedUrlType.Internal) {
                    try {
                        var url = UrlCache.GetUrlFromNodeId((int)model.AddedUrl.NodeId);
                    }
                    catch {

                        // 
                        ModelState.AddModelError("","You have selected an unpublished node. This is not allowed here.");

                        // add config object
                        model.BaseConfigObject = this._urlSelectorConfigObject;

                        // return to view with errors
                        return View(model);
                    }
                }

                // get tuple
                var tuple = model.GetIsValidFromConfigObject(this._urlSelectorConfigObject);

                // check tuple                
                if (tuple.Item1)
                {
                    return View(model.FullPathToAutoRenderPartial, model.GetAutoRenderModel());
                }

                // add
                ModelState.AddModelError("", tuple.Item2);
            }

            // add config object
            model.BaseConfigObject = this._urlSelectorConfigObject;

            // return to view with errors
            return View(model);
        }

        private AddedUrl createAddedUrlFromLink(string text, string href, string title)
        {
            // start
            var output = new AddedUrl();

            // check if link created
            if (!string.IsNullOrEmpty(href))
            {
                // get link
                if (AddedUrl.IsProggyLink(href))
                {
                    output.NodeId = AddedUrl.GetNodeIdFromProggyLink(href);
                    output.UrlType = AddedUrlType.Internal;
                }
                // get mailto
                else if (AddedUrl.IsMailtoLink(href))
                {
                    output.MailToAddress = AddedUrl.GetMailToAddress(href);
                    output.MailToSubject = AddedUrl.GetMailToSubject(href);
                    output.UrlType = AddedUrlType.MailTo;
                }
                else {
                    output.ExternalUrl = href;
                    output.UrlType = AddedUrlType.External;
                }

                // set title
                output.LinkTitle = title;
            }

            // set text
            output.Text = text;

            //
            return output;
        }

        public UrlSelectorConfigObject _urlSelectorConfigObject = new UrlSelectorConfigObject
        {
            RequireDescription = false,
            RequireName = false
        };
    }
}
